<template>
  <div class="container">
    <van-field class="input" v-model="tel" type="tel" label="请输入手机号" />
    <van-button @click="handleNext" type="danger" :disabled="disable" round block
      >下一步</van-button
    >
  </div>
</template>

<script lang="ts" setup>
import { computed, ref, type Ref } from 'vue'
import { docheckphone } from '@/api/login'
import { Dialog } from 'vant'
import { useLoginStore } from '@/stores/login'
import { useRouter } from 'vue-router'
const loginStore = useLoginStore()
const router = useRouter()

const tel: Ref<string> = ref('')

const regTel = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/
const disable = computed(() => {
  // 正则验证成功取反作为disable的值
  return !regTel.test(tel.value)
})

// 点击下一步验证手机号
const handleNext = () => {
  docheckphone({ tel: tel.value }).then((res) => {
    /**
     * res: {
     *  code: "200"
     *  message: "继续注册"
     * }
     */
    if ((res as any).code === '200') {
      Dialog.confirm({
        title: '提示',
        message: `将向手机号：${tel.value}发送短线验证码`,
      })
        .then(() => {
          // on confirm
          loginStore.$patch({ tel: tel.value })
          router.push('/register/step2')
        })
        .catch(() => {
          // on cancel
        })
    } else {
      Dialog.confirm({
        title: '提示',
        message: '手机号已注册，是否去登录',
      })
        .then(() => {
          // on confirm
          router.go(-1)
        })
        .catch(() => {
          // on cancel
        })
    }
  })
}
</script>

<style lang="scss" scoped>
.container {
  padding: 50px 30px;
  background: #fff;
  box-sizing: border-box;
  .input {
    margin-bottom: 100px;
  }
}
</style>
